/**
 * 
 */
package com.iss.card.view.overrideview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.Gallery;

/**
 * @author dtdh
 * 
 */
public class SlideGallery extends Gallery {

	private final static String TAG = SlideGallery.class.getSimpleName();
	private IGalleryScrollEventListener mSlideGalleryScrollListener = null;
	/**
	 * @param context
	 */
	public SlideGallery(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public SlideGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public SlideGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Gallery#onFling(android.view.MotionEvent,
	 * android.view.MotionEvent, float, float)
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;

		
		if (isScrollingLeft(e1, e2)) { // Check if scrolling left
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else { // Otherwise scrolling right
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		if (mSlideGalleryScrollListener != null) {
			mSlideGalleryScrollListener.onScrollEvent(kEvent, this, this.getSelectedItemPosition());
		}
		onKeyDown(kEvent, null);
		return true;

	}

	public void setScrollEventListener(IGalleryScrollEventListener l) {
		mSlideGalleryScrollListener = l;
	}
	
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}

	public interface IGalleryScrollEventListener{
		public void onScrollEvent(int keyCode, AdapterView<?> parent, int position);
	}
}
